package com.kate.mall.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.Query;

import com.kate.mall.order.dao.CartDao;
import com.kate.mall.order.entity.CartEntity;
import com.kate.mall.order.service.CartService;


@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartDao, CartEntity> implements CartService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CartEntity> page = this.page(
                new Query<CartEntity>().getPage(params),
                generatorQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    /**
     * 获取用户选中的所有购物车列表
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public List<CartEntity> queryByUidAndChecked(Integer userId) {
        return this.list(Wrappers.<CartEntity>lambdaQuery().eq(CartEntity::getChecked, true).eq(CartEntity::getDeleted, false));
    }

    @Override
    public void clearGoods(Integer userId) {
        this.remove(Wrappers.<CartEntity>lambdaQuery().eq(CartEntity::getUserId,userId));
    }

    public QueryWrapper generatorQueryWrapper(Map<String, Object> params) {//is_hot=1
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        if (params.containsKey("user_id")) {
            //where is_hot=1
            queryWrapper.eq("user_id", Integer.parseInt((String) params.get("user_id")));
        }
        if (params.containsKey("goods_id")) {
            //where is_hot=1
            queryWrapper.eq("goods_id", Integer.parseInt((String) params.get("goods_id")));
        }
        if (params.containsKey("product_id")) {
            //where is_hot=1
            queryWrapper.eq("product_id", Integer.parseInt((String) params.get("product_id")));
        }
        if (params.containsKey("price")) {
            //where is_hot=1
            queryWrapper.eq("price", Double.parseDouble((String) params.get("price")));
        }
//        queryWrapper.eq("deleted",0);
        return queryWrapper;
    }


}